import Vue from 'vue'
import VueRouter from 'vue-router'
import FileManagement from '../views/FileManagement.vue'
import FirstView from '../views/FirstView.vue'
import LoginView from "../views/LoginView.vue"
import RegisterView from '../views/RegisterView.vue'
import PersonalCenter from '../views/PersonalCenter.vue'
import FileControlView from '../views/pages/FileControlView.vue'
import PersonView from '../views/pages/PersonView.vue'
import VIPControlView from '../views/pages/VIPControlView.vue'
import AccountView from '../views/pages/AccountView.vue'
import MessagesView from '../views/MessagesView.vue'
import UpdatePass from '../views/UpdatePass.vue'
import FileAllView from '../views/filepages/FileAllView.vue'
import FileAnotherView from '../views/filepages/FileAnotherView.vue'
import FileAudioView from '../views/filepages/FileAudioView.vue'
import FileDocumentView from '../views/filepages/FileDocumentView.vue'
import FilePicureView from '../views/filepages/FilePicureView.vue'
import FileVedioView from '../views/filepages/FileVedioView.vue'
import Trade from '../views/Trade.vue'
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    name: 'FirstView',
    component: FirstView
  },
  {
    path: '/file',
    name: 'FileManagement',
    component: FileManagement,
    children:[
      {
        path:'all',
        component:FileAllView
      },
      {
        path:'another',
        component:FileAnotherView
      },
      {
        path:'audio',
        component:FileAudioView
      },
      {
        path:'document',
        component:FileDocumentView
      },
      
      
      {
        path:'picure',
        component:FilePicureView
      },
      
      {
        path:'vedio',
        component:FileVedioView
      },
    ]
  },
  {
    path: '/message',
    name: 'MessagesView',
    component: MessagesView
  },
  {
    path: '/login',
    name: 'LoginView',
    component: LoginView
  },
  {
    path: '/register',
    name: 'RegisterView',
    component: RegisterView
  },
  {
    path: '/updatepass',
    name: 'UpdatePass',
    component: UpdatePass
  },
  {
    path: '/center',
    name: 'PersonalCenter',
    component: PersonalCenter,
    children:[
      {
        path:'account',
        component:AccountView
      },
      {
        path:'file',
        component:FileControlView
      },
      {
        path:'person',
        component:PersonView
      },
      {
        path:'vip',
        component:VIPControlView
      },
    ]
  },
  {
    path: '/trade',
    name: 'Trade',
    component: Trade
  }
]

const router = new VueRouter({
  mode:'history',
  routes,
})
//  路由判断登录 根据路由配置文件的参数
router.beforeEach((to, from, next) => {
  //  to,将要访问的路径
  // from  从哪个路径跳转过来的
  // next 是一个函数,代表放行
   let token = localStorage.getItem('email')
    if(to.path==='/login'||to.path==='/'||to.path==='/register'||to.path==='/updatepass'||to.path==='/trade'){
     return next()
    }else{
      if(!token){
       return next('/login')
      }else{
        next()
      }
    }
 })

export default router
